import {PolicyDetailProvider} from '../../../providers/policy/policy-detail/policy-detail.provider';
import {Component, ViewChild} from '@angular/core';
import {IonicPage, NavController, NavParams, PickerController, DateTime, ToastController, Picker} from 'ionic-angular';
import * as moment from 'moment';
import {TransferRecordGroup} from "../../../model/pages/policy/policy-detail/transfer-record";
import {HttpErrorResponse} from "@angular/common/http";
import {LoadingService} from "../../../services/common/loading.service";

/**
 * Generated class for the TransferRecordsPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
    selector: 'page-transfer-records',
    templateUrl: 'transfer-records.html',
    providers: [PolicyDetailProvider, LoadingService]
})
export class TransferRecordsPage {
    /**
     * 显示的银行转账数据
     * @type {any[]}
     */
    displayTransferList: Array<TransferRecordGroup> = [];

    /**
     * 原始的银行转账数据详情
     * @type {any[]}
     */
    transferList: Array<TransferRecordGroup> = [];

    /**
     * 开始时间
     * @type {string}
     */
    startFilterDate = '';

    /**
     * 结束时间
     * @type {string}
     */
    endFilterDate = '';

    /**
     * 是否在加载数据
     * @type {boolean}
     */
    isLoaded: boolean = false;

    /**
     * 获取数据订阅
     */
    getDataSubscription: any;

    constructor(
        public navCtrl: NavController,
        public navParams: NavParams,
        private policyDetailProvider: PolicyDetailProvider,
        private toastController: ToastController,
        private loadingService: LoadingService) {

    }

    ionViewDidLoad() {
        // 获取id
        let id = this.navParams.get('id');
        if (id != null) {
            // 显示loading动画
            this.loadingService.showLoading();
            // 获取数据
            this.policyDetailProvider.getPolicyAutoPaymentList(id).subscribe(data => {

                this.transferList = data;
                this.displayTransferList = this.transferList;

                // 数据预处理
                this.displayTransferList.forEach(data => {
                    data.transferRecords.forEach(record => {
                        if (record.transferAmount.indexOf(',') >= 0) {
                            var re = /,/gi;
                            record.transferAmount = record.transferAmount.replace(re,'');
                        }
                    })
                });

                this.isLoaded = true;
                // 关闭loading动画
                this.loadingService.dismissLoading();

                // 数据加载完成
                this.isLoaded = true;

            }, (error: HttpErrorResponse) => {
                    // 加载数据结束
                    this.isLoaded = true;
                    // 关闭loading动画
                    this.loadingService.dismissLoading();
            });
        }

    }

    ionViewWillLeave() {
        // 取消订阅数据
        if (this.getDataSubscription) {
            this.getDataSubscription.unsubscribe();
        }
    }

    /**
     * 返回上一级
     */
    goBack() {
        this.navCtrl.pop();
    }

    /**
     * 筛选数据
     */
    filter() {
        // 校验时间范围是否正确
        if (this.endFilterDate === '' && this.startFilterDate === '') {
            let toast = this.toastController.create({
                message: '开始时间和结束时间不能同时为空',
                duration: 2000,
                position: 'middle'
            });
            toast.present();
            // this.cleanFilterDate();
            return;
        }
        if (this.endFilterDate != '' && this.startFilterDate != '' && this.endFilterDate < this.startFilterDate) {
            let toast = this.toastController.create({
                message: '结束时间不能早于开始时间，请重新选择',
                duration: 2000,
                position: 'middle'
            });
            toast.present();
            // this.cleanFilterDate();
            return;
        }

        //  将显示数据还原成原始数据
        this.displayTransferList = JSON.parse(JSON.stringify(this.transferList));

        // 开始筛选
        this.displayTransferList = this.displayTransferList.filter(data => {
            // let num = 0;
            data.transferRecords = data.transferRecords.filter(record => {
                if (this.endFilterDate != '' && this.startFilterDate != '') {
                    return this.startFilterDate <= moment(record.transferDate * 1000).format('YYYY-MM') && this.endFilterDate >= moment(record.transferDate * 1000).format('YYYY-MM');
                }
                if (this.endFilterDate == '') {
                    return this.startFilterDate <= moment(record.transferDate * 1000).format('YYYY-MM');
                }
                if (this.startFilterDate == '') {
                    return this.endFilterDate >= moment(record.transferDate * 1000).format('YYYY-MM');
                }

            });

            return data.transferRecords.length != 0;

        });
    }


}
